
// <copyright file="DynamicToDataTableHelperExtensions.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   The dynamic to data table helper extensions.
// </summary>

namespace SubhadraSolutions.Sharp.Utils.Data.Helpers
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Reflection;

    /// <summary>
    /// The dynamic to data table helper extensions.
    /// </summary>
    public static class DynamicToDataTableHelperExtensions
    {
        #region Public Methods and Operators

        /// <summary>
        /// The export as data table.
        /// </summary>
        /// <param name="queryable">
        /// The queryable.
        /// </param>
        /// <returns>
        /// The <see cref="DataTable"/>.
        /// </returns>
        public static DataTable ExportAsDataTable(this IQueryable queryable)
        {
            return ExportAsDataTable(queryable, queryable.ElementType);
        }

        /// <summary>
        /// The export as data table.
        /// </summary>
        /// <param name="enumerable">
        /// The enumerable.
        /// </param>
        /// <param name="elementType">
        /// The element type.
        /// </param>
        /// <returns>
        /// The <see cref="DataTable"/>.
        /// </returns>
        public static DataTable ExportAsDataTable(this IEnumerable enumerable, Type elementType)
        {
            Type toDataTableType = typeof(DynamicToDataReaderDataTableHelper<>).MakeGenericType(elementType);
            MethodInfo mi = toDataTableType.GetMethod("ExportAsDataTable");
            var table = mi.Invoke(null, new object[] { enumerable }) as DataTable;
            return table;
        }

        /// <summary>
        /// The export as data table.
        /// </summary>
        /// <param name="enumerable">
        /// The enumerable.
        /// </param>
        /// <typeparam name="T">
        /// </typeparam>
        /// <returns>
        /// The <see cref="DataTable"/>.
        /// </returns>
        public static DataTable ExportAsDataTable<T>(this IEnumerable<T> enumerable) where T : class
        {
            return DynamicToDataReaderDataTableHelper<T>.ExportAsDataTable(enumerable);
        }

        #endregion Public Methods and Operators
    }
}